package com.itheima;

import com.bean.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
@WebServlet("/selectStdent")
public class SelectStdent extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String realPath = this.getServletContext().getRealPath("/b.txt");
        BufferedReader br=new BufferedReader(new FileReader(realPath));
        ArrayList<Student> list=new ArrayList<>();
        String line;
        while ((line=br.readLine())!=null){
            Student stu=new Student();
            String[] split = line.split(",");
            stu.setId(split[0]);
            stu.setAge(split[1]);
            stu.setScore(split[2]);
            list.add(stu);
        }
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter pw = resp.getWriter();
        for (Student student : list) {
            pw.write(student.getId()+","+student.getAge()+","+student.getScore());
            pw.write("<br>");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
